package com.dhnsoft.数据结构与算法.栈与队列.队列;

import java.util.Scanner;

/**
 * ClassName: CircleQueueDemo
 * Package: com.dhnsoft.数据结构与算法.栈与队列.队列
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/22 10:07
 * @Version 1.0
 */
public class CircleQueueDemo {
    public static void main(String[] args) {
        CircleQueue queue = new CircleQueue(4);
        char key = ' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);//接收一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    try {
                        System.out.println("输出一个数");
                        int value = scanner.nextInt();
                        queue.addQueue(value);
                    }
                    catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出得数据是%d\n",res);
                        break;
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头数据是%d\n",res);
                        break;
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
            System.out.println("程序退出");
        }
    }
}

class CircleQueue{
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    public CircleQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return rear==front;
    }

    //判断队列是否满
    public boolean isFull(){
        //由于现在是环形队列，需要预留一个值出来判断队列是否已满，可以动态增长数据
        return (rear+1)%maxSize==front;
    }

    //添加数据到队列
    public void addQueue(int n){
        //判断队列是否满了
        if(isFull()){
            throw new RuntimeException("队列满，不能加入数据");
        }
        arr[rear] = n;
        rear = (rear+1)%maxSize;
    }

    //获取队列数据
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，不能获取数据");
        }
        int temp = arr[front];
        front = (front+1)%maxSize;
        return temp;
    }

    //遍历队列
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i=front;i<front+size();i++){
            System.out.printf("arr[%d]=%d\n",(i%maxSize),arr[(i%maxSize)]);
        }
    }

    //获取队列头节点
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空的，没有数据");
        }
        return arr[front];
    }

    //获取队列有效个数
    public int size(){
        return (rear+maxSize-front)%maxSize;
    }
}
